const request = require('request');
const fs = require('fs');
const resDataUrl = './resData/';

function search(name, page = 1) {
  const searchUrl = encodeURI(`https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.center&searchid=47431351136781047&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=20&w=${name}&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0'`);
  console.log('请求地址', searchUrl);
  request.get(searchUrl, function (err, data) {
    if (err) {
      console.log(err);
      return console.log('请求错误');
    }
    fs.writeFile(resDataUrl + 'list.json', data.body, function (err) {
      if (err) {
        console.log(err);
        return console.log('写入list.json失败');
      }
      console.log('请求完成');
    });
  });
}

// search('天下第一等');

function testUrl() {
  const url = 'https://u.y.qq.com/cgi-bin/musicu.fcg?-=getplaysongvkey829247896778311&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0&data="%7B%22req%22:%7B%22module%22:%22CDN.SrfCdnDispatchServer%22,%22method%22:%22GetCdnDispatch%22,%22param%22:%7B%22guid%22:%222649060544%22,%22calltype%22:0,%22userip%22:%22%22%7D%7D%7D'
  request.get(url, function (err, data) {
    if (err) {
      console.log(err);
      return console.log('请求错误');
    }
    fs.writeFile(resDataUrl + 'a.json', data.body, function (err) {
      if (err) {
        console.log(err);
        return console.log('写入list.json失败');
      }
      console.log('请求完成');
    });
  });
}

testUrl();
